package cn.janescott.inner.space.shiro.aop;

import com.alibaba.dubbo.rpc.RpcException;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.*;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 * @author scott
 * @date 2018/8/9
 */
@Aspect
public class AuthenAspect {
    private Logger logger = LoggerFactory.getLogger(getClass());

    /**
     * 切面
     */
    @Pointcut("@annotation(authenAnnotation)")
    public void pointcut(AuthenAnnotation authenAnnotation) {
    }

    /**
     * 前置
     */
    @Before("pointcut(authenAnnotation)")
    public void doBeforeAdvice(JoinPoint joinPoint, AuthenAnnotation authenAnnotation) {
        logger.info("do before advice.");
    }

    /**
     * 环绕
     */
    @Around(value = "pointcut(authenAnnotation)")
    public Object doAroundAdvice(ProceedingJoinPoint joinPoint, AuthenAnnotation authenAnnotation) {
        try {
            Object proceed = joinPoint.proceed();
            return proceed;
        } catch (Throwable ex) {
            logger.error(ex.getMessage(), ex);
        }
        return null;
    }

    /**
     * 后置
     */
    @AfterReturning(pointcut = "pointcut(authenAnnotation)", returning = "returnValue")
    public Object doAfterReturningAdvice(JoinPoint joinPoint, Object returnValue, AuthenAnnotation authenAnnotation) {
        return returnValue;
    }

    /**
     * 异常
     */
    @AfterThrowing(pointcut = "pointcut(authenAnnotation)", throwing = "th")
    public void doAfterThrowingAdvice(JoinPoint joinPoint, AuthenAnnotation authenAnnotation, Throwable th) {
        logger.error(authenAnnotation.message());
        logger.error(authenAnnotation.targetClass().getName());
        if (th instanceof RpcException) {

//        } else if (th instanceof ) {

        }
    }

}
